#include <iostream>
#include <cmath>
int main()
{
  using namespace std;
  double a,b,c,d,n,A,B,C,D,Y1,Y2,rp,ip;
  double temp,arg,t1,t2,t3,t4;
  double rel_x1,rel_x2,rel_x3,maj_x1,maj_x2,maj_x3;
  double x1,x2,x3,i;
  cout<<"input n(方程的次数）:";
  cin>>n;
  if(n==2)
 {
   cout<<"input a,b,c(方程系数):";
   cin>>a>>b>>c;
   if(a==0)
    {x1 = -c/b;
      cout<<"方程为一次方程："<<x1<<endl;
    }
   else
    {
      d = b*b-4*a*c;
      if(d >=0)
       {
         if(d==0)
          {
            x1 = -b/(2*a);

            cout<<"只有一个根:"<<x1<<endl;
          }

         else
          {
	          x1 = (-b+sqrt(d))/(2*a);
	          x2 = (-b-sqrt(d))/(2*a);
	          cout<<"有两个根:"<<x1<<"and"<<x2<<endl;
          }
       }
      else
       {rp = -b/(2*a);
        ip = sqrt(-d)/(2*a);
        x1 = rp+ip;
        x2 = rp-ip;
        cout<<"有两个虚根:"<<x1<<"i"<<"and"<<x2<<"i"<<endl;

       }
    }
 }
 else if (n==3)
	 cout<<"这个方程为3次方程:"<<endl;
	 cout<<"input a,b,c,d(方程系数）:"<<endl;
	 cin>>a>>b>>c>>d;
	      A=b*b-3*a*c;
        B=b*c-9*a*d;
        C=c*c-3*b*d;
        D=B*B-4*A*C;
        Y1=A*b+3*a*(-B+pow((B*B-4*A*C),0.5))/2;
        Y2=A*b+3*a*(-B-pow((B*B-4*A*C),0.5))/2;
         if(A==0&&B==0)
        {
            cout<<"x1=x2=x3="<<-b/(3*a);//盛金公式1：方程有一个三重实数根
            cout<<"一元三次方程组解为"<<"x1="<<x1<<endl;
            cout<<"一元三次方程组解为"<<"x2="<<x2<<endl;
            cout<<"一元三次方程组解为"<<"x3="<<x2<<endl;
        }
        else if(D>0)//盛金公式2：方程有一个实数根和一对共轭复根
        {
            if(Y2<0) t2=-pow(-Y2,pow(3.0,-1));
            else t2=pow(Y2,pow(3.0,-1));
            if(Y1<0) t1=-pow(-Y1,pow(3.0,-1));
            else t1=pow(Y1,pow(3.0,-1));
            x1=-(b+t1+t2)/(3*a);
            rel_x2=rel_x3=(-2*b+t1+t2)/(6*a);
            maj_x2=(pow(3.0,0.5)*(t1-t2))/(6*a);
            maj_x3=-(pow(3.0,0.5)*(t1-t2))/(6*a);
            /*printf("方程的解为:x1=%f\n",x1);
            printf("方程的解为:x2=%f+(%f)*i\n",rel_x2,maj_x2);
            printf("方程的解为:x3=%f+(%f)*i\n",rel_x3,maj_x3);*/
            cout<<"一元三次方程组解为"<<"x1="<<x1<<endl;
            cout<<"一元三次方程组解为"<<"x2="<<rel_x2<<'+'<<maj_x2<<"*i"<<endl;
            cout<<"一元三次方程组解为"<<"x3="<<rel_x3<<'-'<<maj_x3<<"*i"<<endl;
        }
        else if(D==0)//盛金公式3：方程有三个实数根，其中有一个两重根
        {
                cout<<"方程的解为:x1="<<(-b/a+B/A)<<endl;
                cout<<"方程的解为:x2=x3="<<(-B/2*A)<<endl;
                cout<<"一元三次方程组解为"<<"x1="<<x1<<endl;
                cout<<"一元三次方程组解为"<<"x2="<<x2<<endl;
                cout<<"一元三次方程组解为"<<"x3="<<x2<<endl;
        }
        else//盛金公式4：方程有三个不相等的实数根
        {
            if(A<0)
            	{t3=-pow(-A,1/2);t4=-pow(-A,3/2);}
            else 
            	{t3=pow(A,1/2);t4=pow(A,3/2);}
			      arg=acos(2*A*b-3*a*B)/(2*t4);
            x1=(-B-2*t3*cos(arg/3))/(3*a);
            x2=(-b+t3*(cos(arg/3)+pow(3.0,1/2))*sin(arg/3));
            x3=(-b+t3*(cos(arg/3)-pow(3.0,1/2))*sin(arg/3));
            cout<<"方程的解为:x1="<<x1<<endl;
            cout<<"方程的解为:x2="<<x2<<endl;
            cout<<"方程的解为:x3="<<x3<<endl;
            cout<<"一元三次方程组解为"<<"x1="<<x1<<endl;
            cout<<"一元三次方程组解为"<<"x2="<<x2<<endl;
            cout<<"一元三次方程组解为"<<"x3="<<x2<<endl;
        }
	return 0;
}

